package com.usian.ai.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.ai.common.Result;
import com.usian.ai.mapper.CandidateMapper;
import com.usian.ai.mapper.JobMapper;
import com.usian.ai.pojo.Candidate;
import com.usian.ai.pojo.Job;
import com.usian.ai.pojo.vo.CandidateReqVo;
import com.usian.ai.pojo.vo.CandidateSaveReqVo;
import com.usian.ai.service.CandidateService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

/**
 * @TODO:
 * @Author:baimu
 * @Date:2024 12 05 11 26
 **/

@Service
public class CandidateServiceImpl extends ServiceImpl<CandidateMapper, Candidate> implements CandidateService {


    @Autowired
    JobMapper jobMapper;
    @Override
    public Result listCandidate(CandidateReqVo candidateReqVo) {

        QueryWrapper<Candidate> objectQueryWrapper = new QueryWrapper<>();
//        if(candidateReqVo.getReaLName()!=null){
//            objectQueryWrapper.lambda().eq(Candidate::getRealName,candidateReqVo.getReaLName());
//        }
//
//        if(candidateReqVo.getMobile()!=null){
//            objectQueryWrapper.lambda().eq(Candidate::getMobile,candidateReqVo.getMobile());
//        }

        objectQueryWrapper.lambda().eq(StrUtil.isNotBlank(candidateReqVo.getRealName()),Candidate::getRealName,candidateReqVo.getRealName());
        objectQueryWrapper.lambda().eq(StrUtil.isNotBlank(candidateReqVo.getMobile()),Candidate::getMobile,candidateReqVo.getMobile());


        Page<Candidate> candidatePage = new Page<>(candidateReqVo.getPageNumber(),candidateReqVo.getPageSize());
        Page<Candidate> candidatePage1 = this.baseMapper.selectPage(candidatePage, objectQueryWrapper);


        List<Job> jobs = jobMapper.selectList(null);
        HashMap<Integer, String> objectObjectHashMap = new HashMap<>();

        jobs.forEach(job -> {
            Integer id = job.getId();
            String jobName = job.getJobName();
            objectObjectHashMap.put(id,jobName);
        });


        //用jobid 查询jobname

        candidatePage1.getRecords().forEach(candidate -> {
            Integer jobId = candidate.getJobId();
            String name = objectObjectHashMap.get(jobId);
            candidate.setJobName(name);
        });



        return Result.ok(candidatePage1);
    }

    @Override
    public Result saveCandidate(CandidateSaveReqVo candidateSaveReq) {
        Candidate candidate = new Candidate();
        candidate.setCreatedTime(LocalDateTime.now());
        candidate.setUpdatedTime(LocalDateTime.now());
        //对象复制
        BeanUtils.copyProperties(candidateSaveReq,candidate);
        int insert = this.baseMapper.insert(candidate);
        return Result.ok(insert);
    }
}
